layui.use(['form','jquery','table','layer'],function(){
    let form = layui.form,
        $ = layui.jquery,
        table = layui.table,
        layer = layui.layer;

    //返回上一页面
    $('.back_btn').click(function(){
        window.history.go(-1);
    });

    //取URL传过来的值
    var loc=location.href;
    var n1=loc.length;//地址的总长度
    var n2=loc.indexOf("=");//取得=号的位置
    var n3=loc.indexOf("&");//取得&号的位置
    var id=decodeURI(loc.substr(n2+1, n3-n2-1));//从=号后面到&的内容，即id的值

    //加载数据
    let contactTable = table.render({
        elem: '#contactList'
        , url: '/customer-contact/list/'+id
        , page: true
        , cols: [[
            {field: 'id', title: 'ID',width:55,sort:true},
            {field: 'name', title: '姓名'},
            {field: 'sex', title: '性别',width:100},
            {field: 'job', title: '职位'},
            {field: 'email', title: '电子邮箱'},
            {field: 'tel', title: '手机'},
            {field: 'remark', title: '备注'},
            {title: '操作', fixed: 'right', toolbar: "#contactTool"}
            ]]
    });

    $('.add_btn').click(function () {
        layer.open({
            type:2
            ,content:'/views/customerContact/save.jsp?id='+id
            ,skin:'layui-layer-molv'
            ,title:"添加联系人"
            ,area:["650px","560px"]
        });
    });

    //工具栏：编辑和删除
    table.on('tool(contactList)',function (obj) {
        switch (obj.event){
            case 'edit_btn':
                layer.open({
                    type:2,
                    content:"/customer-contact/editPage/"+obj.data.id,
                    skin:'layui-layer-molv',
                    title:"编辑联系人",
                    area:["650px","550px"]
                });
                break;
            case 'del_btn':
                layer.confirm('你确定要删除['+obj.data.name+']吗？',{icon:3,anim: 4,skin:'layui-layer-molv'},function () {
                    $.post('customer-contact/del/'+obj.data.id,function (data) {
                        contactTable.reload();
                        layer.msg(data.msg);
                    });
                });
        }
    });

    //提交添加后关闭并刷新页面
    form.on('submit(submit)',function () {
        window.parent.location.reload(contactTable); //刷新父页面
        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        parent.layer.close(index);  // 关闭layer
    })

    $('.close').click(function () {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    });

    //刷新单选框radio，解决单选框不显示的问题
    layui.use('form',function(){
        var form = layui.form;
        form.render('radio');
    });

});